#ifndef _SETTINGS_H
#define _SETTINGS_H

extern void nvsInit();

void saveSettingByItem(int itemNum) {
	nvs_handle_t nvsH;
	ESP_ERROR_CHECK(nvs_open("settings", NVS_READWRITE, &nvsH));
	if(strcmp(stGroup[itemNum].valueType, "str") == 0) {
		ESP_ERROR_CHECK(nvs_set_str(nvsH, stGroup[itemNum].itemName, stGroup[itemNum].strValue));
	}
	else if(strcmp(stGroup[itemNum].valueType, "int") == 0) {
		ESP_ERROR_CHECK(nvs_set_i32(nvsH, stGroup[itemNum].itemName, stGroup[itemNum].intValue));
	}
	ESP_ERROR_CHECK(nvs_commit(nvsH));
	nvs_close(nvsH);
}

void loadSettingByItem(int itemNum) {
	nvs_handle_t nvsH;
	err = nvs_open("settings", NVS_READONLY, &nvsH);
	if(err == ESP_OK) {
		if(strcmp(stGroup[itemNum].valueType, "str") == 0 ) {
			size_t valueSize = 64;
			// err = nvs_get_str(nvsH, stGroup[itemNum].itemName, &stGroup[itemNum].strValue, &valueSize);
			nvs_get_str(nvsH, stGroup[itemNum].itemName, &stGroup[itemNum].strValue, &valueSize);
			// log("load settings err = %s, size = %d, settigns item = %s, value = %s", esp_err_to_name(err), valueSize, stGroup[itemNum].itemName, stGroup[itemNum].strValue);
			
		}
		else if(strcmp(stGroup[itemNum].valueType, "int") == 0) {
			uint32_t *intValue;
			ESP_ERROR_CHECK(nvs_get_i32(nvsH, stGroup[itemNum].itemName, &intValue));
			stGroup[itemNum].intValue = intValue;
		}
	}
	
	nvs_close(nvsH);
}

void saveSettings() {
	nvs_handle_t nvsH;
	ESP_ERROR_CHECK(nvs_open("settings", NVS_READWRITE, &nvsH));
	for(int i = 0; i < ST_CNT; i++) {
		if(strcmp(stGroup[i].valueType, "str") == 0) {
			ESP_ERROR_CHECK(nvs_set_str(nvsH, stGroup[i].itemName, stGroup[i].strValue));
		}
		else if(strcmp(stGroup[i].valueType, "int") == 0) {
			ESP_ERROR_CHECK(nvs_set_i32(nvsH, stGroup[i].itemName, stGroup[i].intValue));
			// ESP_ERROR_CHECK(nvs_set_i32(nvsH, stGroup[i].itemName, stGroup[i].intValue));
			// err = nvs_set_i32(nvsH, stGroup[i].itemName, stGroup[i].intValue);
			// log("err = %d", err);
			// if(err != ESP_OK) {
			// 	log("err = %d", err);
			// }
		}
	}
	ESP_ERROR_CHECK(nvs_commit(nvsH));
	nvs_close(nvsH);
}

void loadSettings() {
	nvs_handle_t nvsH;
	err = nvs_open("settings", NVS_READONLY, &nvsH);
	LOG_ERR(err);
	if(err == ESP_OK) {
		for(int i = 0; i < ST_CNT; i ++) {
			if(strcmp(stGroup[i].valueType, "str") == 0 ) {
				size_t valueSize = sizeof(stGroup[i].strValue);
				LOG_ERR(nvs_get_str(nvsH, stGroup[i].itemName, &stGroup[i].strValue, &valueSize));
			}
			else if(strcmp(stGroup[i].valueType, "int") == 0) {
				uint32_t *intValue;
				LOG_ERR(nvs_get_i32(nvsH, stGroup[i].itemName, &intValue));
				stGroup[i].intValue = intValue;
			}
		}
	}
	nvs_close(nvsH);
}

void resetSettings() {
	for(int i = 0; i < ST_CNT; i++) {
		if(strcmp(stGroup[i].valueType, "str") == 0 ) {
			strcpy(stGroup[i].strValue, stGroup[i].defaultStrValue);
			// ESP_ERROR_CHECK(nvs_set_str(nvsH, stGroup[i].itemName, stGroup[i].strValue));
		}
		else if(strcmp(stGroup[i].valueType, "int") == 0) {
			stGroup[i].intValue = stGroup[i].defaultIntValue;
			// ESP_ERROR_CHECK(nvs_set_i32(nvsH, stGroup[i].itemName, stGroup[i].intValue));
		}
	}
	saveSettings();
}

// 测试用的
void printSettings() {
	DELAY(100);
	// loadSettings();
	for(int i = 0; i < ST_CNT; i++) {
		printf("settings item = %s, ", stGroup[i].itemName);
		if(strcmp(stGroup[i].valueType, "str") == 0) {
			printf(" value = %s\n", stGroup[i].strValue);
		}
		else if(strcmp(stGroup[i].valueType, "int") == 0) {
			printf(" value = %d\n", stGroup[i].intValue);
		}
	}
}

#endif